package com.ph.irp.sell.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ph.irp.base.assistantmaterial.dao.PBaseAssistantmaterialDeliveryaddressDao;
import com.ph.irp.base.assistantmaterial.dao.PBaseAssistantmaterialDeliverymethodDao;
import com.ph.irp.base.currency.dao.PBaseCurrencyDAO;
import com.ph.irp.base.settlement.dao.PBaseSettlementDAO;
import com.ph.irp.base.verifyproject.dao.PBaseVerifyprojectCandiateDao;
import com.ph.irp.base.verifyproject.dao.PBaseVerifyprojectCustomerDao;
import com.ph.irp.base.verifyproject.dao.PBaseVerifyprojectDepartmentDao;
import com.ph.irp.dto.SellOrderDTO;
import com.ph.irp.model.PBaseAssistantmaterialDeliveryaddress;
import com.ph.irp.model.PBaseAssistantmaterialDeliverymethod;
import com.ph.irp.model.PBaseCurrency;
import com.ph.irp.model.PBaseSettlement;
import com.ph.irp.model.PBaseVerifyprojectCandiate;
import com.ph.irp.model.PBaseVerifyprojectCustomer;
import com.ph.irp.model.PBaseVerifyprojectDepartment;
import com.ph.irp.model.PSellSellorder;
import com.ph.irp.model.PSellSellorderdetail;
import com.ph.irp.sell.dao.SellOrderDAO;
import com.ph.irp.sell.dao.SellOrderDetailDAO;
import com.ph.irp.sell.service.SellOrderService;

public class SellOrderServiceImpl implements SellOrderService {
	private SellOrderDAO sellOrderDaoProxy;
	private SellOrderDetailDAO sellOrderDetailDaoProxy;
	private PBaseVerifyprojectCustomerDao customerDaoProxy;
	private PBaseVerifyprojectCandiateDao candiateDaoProxy;
	private PBaseCurrencyDAO currencyDaoProxy;
	private PBaseVerifyprojectDepartmentDao departmentDaoProxy;
	private PBaseAssistantmaterialDeliverymethodDao deliveryMethodDaoProxy;
	private PBaseAssistantmaterialDeliveryaddressDao deliveryAddressDaoProxy;
	private PBaseSettlementDAO settlementProxy;
	
	
	public PBaseSettlementDAO getSettlementProxy() {
		return settlementProxy;
	}
	public void setSettlementProxy(PBaseSettlementDAO settlementProxy) {
		this.settlementProxy = settlementProxy;
	}
	@Override
	/**
	 * 增加销售订单，先增加销售订单然后逐条增加销售明细单
	 */
	public void addSellOrder(PSellSellorder sellOrder,
			List<PSellSellorderdetail> sellOrderDetailLists) throws Throwable {
		//保存销售订单
		sellOrderDaoProxy.addSellOrder(sellOrder);
		//得到销售订单的Id
		int sellOrderId = sellOrderDaoProxy.getSellOrderIdByCode(sellOrder.getSellOrderCode());
		//逐条增加销售明细
		for(PSellSellorderdetail sellOrderDetail :sellOrderDetailLists){
			//销售订单的id存入销售明细中
			sellOrderDetail.setSellOrderId(sellOrderId);
			//增加销售明细
			sellOrderDetailDaoProxy.addSellOrderDetail(sellOrderDetail);
		}
	}
	/**
	 * 增加销售明细
	 */
	public void addSellOrderDetail(PSellSellorderdetail sellOrderDetail)
			throws Throwable {
		sellOrderDetailDaoProxy.addSellOrderDetail(sellOrderDetail);
		
	}
	/**
	 * 查询所有销售订单
	 */
	@Override
	public List<SellOrderDTO> getAllSellOrder() throws Throwable {
		List<PSellSellorder> sellOrderList = sellOrderDaoProxy.getAllSellOrder();
		List<SellOrderDTO> sellOrderDTOList = new  ArrayList<SellOrderDTO>();
		for(int i=0;i<sellOrderList.size();i++){
			SellOrderDTO sellOrderDTO = new SellOrderDTO();
			sellOrderDTO.setSellOrder(sellOrderList.get(i));
			PBaseVerifyprojectCustomer customer = customerDaoProxy.getCustomerById(sellOrderList.get(i).getCustomerID());
			sellOrderDTO.setCustomer(customer);
			PBaseVerifyprojectDepartment department = departmentDaoProxy.getDepartmentById(sellOrderList.get(i).getDepartmentID());
			sellOrderDTO .setDepartment(department);
			PBaseVerifyprojectCandiate businesser = candiateDaoProxy.getCandiateById(sellOrderList.get(i).getBusinesserID());
			sellOrderDTO.setBusinesser(businesser);
			PBaseVerifyprojectCandiate manager = candiateDaoProxy.getCandiateById(sellOrderList.get(i).getManagerID());
			sellOrderDTO.setManager(manager);
			PBaseSettlement settlement = settlementProxy.getSettlementById(sellOrderList.get(i).getSettlementId());
			sellOrderDTO.setSettlement(settlement);
			PBaseAssistantmaterialDeliveryaddress deliveryAddress = deliveryAddressDaoProxy.getDeliveryaddressById(sellOrderList.get(i).getDeliveryAddressId());
			sellOrderDTO.setDeliveryAddress(deliveryAddress);
			PBaseAssistantmaterialDeliverymethod deliverymethod = deliveryMethodDaoProxy.getDeliverymethodId(sellOrderList.get(i).getDeliverymethodID());
			sellOrderDTO.setDeliverymethod(deliverymethod);
			PBaseCurrency currency = currencyDaoProxy.getCurrencyByID(sellOrderList.get(i).getCurrencyId());
			sellOrderDTO.setCurrency(currency);
			sellOrderDTOList.add(sellOrderDTO);
		}
		return sellOrderDTOList;
	}
	/**
	 * 根据查询条件查询销售订单
	 */
	@Override
	public List<SellOrderDTO> getSellOrderByCondition(PSellSellorder sellOrderModel,String beginDate,String endDate) throws Throwable {
		List<PSellSellorder> sellOrderList = sellOrderDaoProxy.getSellOrderByCondition(sellOrderModel,beginDate,endDate);
		List<SellOrderDTO> sellOrderDTOList = new  ArrayList<SellOrderDTO>();
		for(int i=0;i<sellOrderList.size();i++){
			SellOrderDTO sellOrderDTO = new SellOrderDTO();
			sellOrderDTO.setSellOrder(sellOrderList.get(i));
			PBaseVerifyprojectCustomer customer = customerDaoProxy.getCustomerById(sellOrderList.get(i).getCustomerID());
			sellOrderDTO.setCustomer(customer);
			PBaseVerifyprojectDepartment department = departmentDaoProxy.getDepartmentById(sellOrderList.get(i).getDepartmentID());
			sellOrderDTO .setDepartment(department);
			PBaseVerifyprojectCandiate businesser = candiateDaoProxy.getCandiateById(sellOrderList.get(i).getBusinesserID());
			sellOrderDTO.setBusinesser(businesser);
			PBaseVerifyprojectCandiate manager = candiateDaoProxy.getCandiateById(sellOrderList.get(i).getManagerID());
			sellOrderDTO.setManager(manager);
			PBaseSettlement settlement = settlementProxy.getSettlementById(sellOrderList.get(i).getSettlementId());
			sellOrderDTO.setSettlement(settlement);
			PBaseAssistantmaterialDeliveryaddress deliveryAddress = deliveryAddressDaoProxy.getDeliveryaddressById(sellOrderList.get(i).getDeliveryAddressId());
			sellOrderDTO.setDeliveryAddress(deliveryAddress);
			PBaseAssistantmaterialDeliverymethod deliverymethod = deliveryMethodDaoProxy.getDeliverymethodId(sellOrderList.get(i).getDeliverymethodID());
			sellOrderDTO.setDeliverymethod(deliverymethod);
			PBaseCurrency currency = currencyDaoProxy.getCurrencyByID(sellOrderList.get(i).getCurrencyId());
			sellOrderDTO.setCurrency(currency);
			sellOrderDTOList.add(sellOrderDTO);
		}
		return sellOrderDTOList;
	}
	@Override
	public void addSell(PSellSellorder sellOrder) throws Throwable {
		sellOrderDaoProxy.addSellOrder(sellOrder);
		
	}
	public SellOrderDAO getSellOrderDaoProxy() {
		return sellOrderDaoProxy;
	}
	public void setSellOrderDaoProxy(SellOrderDAO sellOrderDaoProxy) {
		this.sellOrderDaoProxy = sellOrderDaoProxy;
	}
	public SellOrderDetailDAO getSellOrderDetailDaoProxy() {
		return sellOrderDetailDaoProxy;
	}
	public void setSellOrderDetailDaoProxy(
			SellOrderDetailDAO sellOrderDetailDaoProxy) {
		this.sellOrderDetailDaoProxy = sellOrderDetailDaoProxy;
	}
	public PBaseVerifyprojectCustomerDao getCustomerDaoProxy() {
		return customerDaoProxy;
	}
	public void setCustomerDaoProxy(PBaseVerifyprojectCustomerDao customerDaoProxy) {
		this.customerDaoProxy = customerDaoProxy;
	}
	public PBaseVerifyprojectCandiateDao getCandiateDaoProxy() {
		return candiateDaoProxy;
	}
	public void setCandiateDaoProxy(PBaseVerifyprojectCandiateDao candiateDaoProxy) {
		this.candiateDaoProxy = candiateDaoProxy;
	}
	public PBaseCurrencyDAO getCurrencyDaoProxy() {
		return currencyDaoProxy;
	}
	public void setCurrencyDaoProxy(PBaseCurrencyDAO currencyDaoProxy) {
		this.currencyDaoProxy = currencyDaoProxy;
	}
	public PBaseVerifyprojectDepartmentDao getDepartmentDaoProxy() {
		return departmentDaoProxy;
	}
	public void setDepartmentDaoProxy(
			PBaseVerifyprojectDepartmentDao departmentDaoProxy) {
		this.departmentDaoProxy = departmentDaoProxy;
	}
	public PBaseAssistantmaterialDeliverymethodDao getDeliveryMethodDaoProxy() {
		return deliveryMethodDaoProxy;
	}
	public void setDeliveryMethodDaoProxy(
			PBaseAssistantmaterialDeliverymethodDao deliveryMethodDaoProxy) {
		this.deliveryMethodDaoProxy = deliveryMethodDaoProxy;
	}
	public PBaseAssistantmaterialDeliveryaddressDao getDeliveryAddressDaoProxy() {
		return deliveryAddressDaoProxy;
	}
	public void setDeliveryAddressDaoProxy(
			PBaseAssistantmaterialDeliveryaddressDao deliveryAddressDaoProxy) {
		this.deliveryAddressDaoProxy = deliveryAddressDaoProxy;
	}
	

}
